package grape.attend.service.classe.api;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import grape.attend.service.classe.po.Classe;
import grape.attend.service.group.po.Group;
import grape.common.exception.runtime.RBaseException;
import grape.common.service.common.IBaseService;
import grape.common.service.trans.ITransService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 班次设置表 服务类
 * </p>
 *
 * @author yangwei
 * @since 2020-05-08
 */
public interface IClasseService extends IBaseService<Classe> , ITransService<String,String> {
    public static final String trans_classeName = "trans_classeName";
    /**
     * 根据考勤组id获取班次
     * @param groupId
     * @return
     */
    List<Classe> getByGroupId(String groupId);

    /**
     * 根据考勤组id和班次id获取班次
     * @param groupId 考勤组id，看似多余其实获取该考勤组下的班次
     * @param classeId
     * @return
     */
    Classe getByGroupIdAndClasseId(String groupId, String classeId);

    /**
     * 是否上班时间有交叉
     * @param classes
     * @return
     */
    public static List<Classe> isCrossTime(List<Classe> classes) {
        List<Classe> result = null;
        if (classes != null && !classes.isEmpty()) {
            for (Classe aClass : classes) {
                long aStartTime = DateUtil.parseTimeToday(aClass.getStartTime()).getTime();
                long aEndTime = DateUtil.parseTimeToday(aClass.getEndTime()).getTime();
                if(aEndTime < aStartTime){
                    aEndTime = aEndTime + DateUnit.DAY.getMillis();
                }

                for (Classe classe : classes) {
                    long startTime = DateUtil.parseTimeToday(classe.getStartTime()).getTime();
                    long endTime = DateUtil.parseTimeToday(classe.getEndTime()).getTime();
                    if(endTime < startTime){
                        endTime = endTime + DateUnit.DAY.getMillis();
                    }
                    if(between(aStartTime,aEndTime,startTime)
                            || between(aStartTime,aEndTime,endTime)
                            || between(startTime,endTime,aStartTime)
                            || between(startTime,endTime,aEndTime)
                    ){
                        result = new ArrayList<>(2);
                        result.add(aClass);
                        result.add(classe);
                        return result;
                    }
                }
            }
        }
        return result;
    }
    public static boolean between(long first,long end ,long value){
        return first < value && value < end;
    }

    @Override
    default boolean support(String type){
        return isEqualAny(type,trans_classeName);
    }

    @Override
    default String trans(String type, String key){
        if (isEqual(type,trans_classeName)) {
            Classe po = getById(key);
            return po.getName();
        }
        return null;
    }
}
